﻿using System;
using {namespace}.DataAccess;
namespace {namespace}
{
    public partial class {ClassName}
    {
	#region "Private Variables"    
	
	private bool _wasChanged = true;	//Variable used to handle WasChanged property
	private bool _isNew = true;			//Variable used to detect new created elements
	
	private {ClassName}Data _ownData = new {ClassName}Data();
	
	{ForeignKeysPrivateVariables}
	
	#endregion
	
	#region "Constructors"    

	public {ClassName}()
	{
		_isNew = true;
	}	
{Constructors}

	#endregion
	
	#region "Public Properties"    
	
	public {ClassName}Data Data
		{
			get { return _ownData; }
		}
		
	public bool WasChanged
		{
			get { return _wasChanged; }
		}
	
{PublicProperties}

{ForeignKeysProperties}

	#endregion
	
	#region "Public Methods"   
		public void Save()
		{
			using({ClassName}DAL dal = new {ClassName}DAL())
			{
				if(_isNew) 
					dal.Insert(_ownData); 
				else 
					dal.Update(_ownData);
				_isNew = false;
				_wasChanged = false;
					
			}
		}
	public bool IsLoaded
	{
		get{return _ownData != null;}
	}
	public void Remove()
	{
		using({ClassName}DAL dal = new {ClassName}DAL())
			{
				if(_isNew) 
					throw new ApplicationException("It is not possible to remove unsaved data!");
				
				dal.Remove(_ownData);
				_isNew = false;
				_wasChanged = true;
			}
	}
	#endregion
	
    }
}